<template>
  <!-- 参数集合管理 -->
  <div class="filterContent">
    <a-form :model="filterForm" layout="inline">
      <a-form-item label="参数集合名称：">
        <a-input
          v-model:value="filterForm.name"
          class="formInput"
          placeholder="请输入"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="参数集合编码：">
      <a-input
          v-model:value="filterForm.code"
          class="formInput"
          placeholder="请输入"
          allow-clear
      />
    </a-form-item>
      <a-button type="primary" class="filterBtn" @click="handleFilter">查询</a-button>
      <a-button type="primary" class="filterBtn greenBtn" @click="handleParam"
        >新增</a-button
      >
    </a-form>
  </div>
  <div class="tableContent">
    <a-table
      :dataSource="tableData"
      :columns="columns"
      size="small"
      :scroll="{ x: 'max-content' }"
      @resizeColumn="handleResizeColumn"
      :pagination="false"
      :loading="tableLoading"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'handle'">
          <a-popconfirm ok-text="确认" cancel-text="取消" @confirm="confirmDel(record)">
            <template #title> 是否确定删除该参数？ </template>
            <a-button type="link" danger>删除</a-button>
          </a-popconfirm>
         
          <a-button type="link" @click="handleEdit(record)">编辑</a-button>
          <a-button type="link" @click="handleParamList(record)">查看</a-button>
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="filterForm.current"
      v-model:page-size="filterForm.size"
      :total="total"
      position="bottomRight"
      show-size-changer
      @change="getParamCodeCollectPage"
      :show-total="total => `总共 ${total} 条`"
    >
      <template #buildOptionText="props">
        <span>{{ props.value }}条/页</span>
      </template>
    </a-pagination>
  </div>
  <!-- 新增设备/器部件 -->
  <AddParam
    :addParamVisible="addParamVisible"
    :rowData="rowData"
    :isEdit="isEdit"
    @update:addParamVisible="(val) => (addParamVisible = val)"
    @getParamCodeCollectPage="handleFilter"
  />
  <!-- 绑定模版参数列表 -->
  <ParamList 
    :paramListVisible="paramListVisible"
    :rowData="rowData"
    @update:paramListVisible="(val) => (paramListVisible = val)"
    @getParamCodeCollectPage="handleFilter"
  />
</template>
<script setup>

import AddParam from "./conpoments/addParam.vue";
import ParamList from "./conpoments/paramList.vue";

const $api = inject("$api");
const filterForm = ref({
  name: null,
  code: null,
  current: 1,
  size: 10,
});
const labelCol = { style: { width: "100px" } };
const total = ref();
const addParamVisible = ref(false);
const paramListVisible = ref(false);
const tableData = ref([]);
const rowData = ref({});
const isEdit = ref(false);
const columns = ref([
  {
    title: "参数集合名称",
    dataIndex: "name",
    key: "name",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "参数集合编码",
    dataIndex: "code",
    key: "code",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    width: 150,
    resizable: true,
    align: "center",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    width: 180,
    resizable: true,
    align: "center",
  }, 
  {
    title: "操作",
    dataIndex: "handle",
    key: "handle",
    width: 150,
    resizable: true,
    align: "center",
    fixed: "right",
  },
]);
function handleResizeColumn(w, col) {
  col.width = w;
}
const handleParam = () => {
  isEdit.value = false;
  addParamVisible.value = true;
};
const handleEdit = (data) => {
  rowData.value = data;
  isEdit.value = true;
  addParamVisible.value = true;
}
const handleParamList = (data) => {
  rowData.value = data;
  paramListVisible.value = true;
}
const confirmDel = async (data) => {
  const res = await $api.paramCodeCollectDel(data.id);
  if (res && res.code === 0) {
    message.success("删除成功");
    getParamCodeCollectPage();
  }
}
const tableLoading = ref(false);
const getParamCodeCollectPage  = async () => {
  const params = {
    ...filterForm.value,
  };
  tableLoading.value = true;
  const res = await $api.paramCodeCollectPage(params);
  tableLoading.value = false;
  if (res && res.code === 0) {
    tableData.value = res.data.records;
    total.value = res.data.total;
  }
}
const handleFilter = () => {
  filterForm.value.current = 1;
  filterForm.value.size = 10;
  getParamCodeCollectPage();
}
onMounted(() => {
  getParamCodeCollectPage();
});
</script>

<style lang="less" scoped>
.ant-table {
  .ant-btn {
    padding: 4px 5px;
  }
}
.formInput {
  width: 155px;
}
.filterBtn {
  margin: 0 20px 20px 0;
  &:last-child {
    margin-right: 0;
  }
}
.tableContent {
  border-radius: 4px;
  background-color: #141414;
  padding: 8px;
}
</style>